package city.spring.controller.mealService;

import city.spring.domain.entity.mealService.SendAreaEntity;
import city.spring.modules.bed.entity.BedRoomEntity;
import city.spring.modules.bed.service.BedRoomService;
import city.spring.service.mealService.SendAreaService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.google.common.collect.ArrayListMultimap;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("send-area")
public class SendAreaApiController extends ApiController {

    private final SendAreaService sendAreaService;
    private final BedRoomService bedRoomService;

    private final List<SFunction<BedRoomEntity, ?>> entityOrderFields;

    public SendAreaApiController(SendAreaService sendAreaService, BedRoomService bedRoomService) {
        this.sendAreaService = sendAreaService;
        this.bedRoomService = bedRoomService;
        this.entityOrderFields = new ArrayList<>();
        entityOrderFields.add(BedRoomEntity::getId);
        entityOrderFields.add(BedRoomEntity::getBedFloorId);
        entityOrderFields.add(BedRoomEntity::getRoomTypeId);
        entityOrderFields.add(BedRoomEntity::getCode);
        entityOrderFields.add(BedRoomEntity::getName);
        entityOrderFields.add(BedRoomEntity::getDirection);
        entityOrderFields.add(BedRoomEntity::getGmtCreate);
        entityOrderFields.add(BedRoomEntity::getGmtModified);
        entityOrderFields.add(BedRoomEntity::getAreaId);
    }

    @GetMapping("getAllArea")
    public Object getAllType() {
        List<SendAreaEntity> typeEntities = sendAreaService.list();
        List<BedRoomEntity> valueEntities = bedRoomService.list();

        ArrayListMultimap<String, BedRoomEntity> multimap = ArrayListMultimap.create();
        valueEntities.forEach(bedRoomEntity -> multimap.put(bedRoomEntity.getAreaId(), bedRoomEntity));
        typeEntities.forEach(sendAreaEntity ->
                sendAreaEntity.setChildren(multimap.get(sendAreaEntity.getId())));


        return success(typeEntities);
    }

    @GetMapping("sendArea-{sendId}")
    public Object listValues(@PageableDefault(sort = {"sorted", "gmtCreate"}) Pageable pageable, BedRoomEntity entity, @PathVariable String sendId) {
        return success(loadDataDictionaryEntity(pageable, entity, sendId).getChildren());
    }

    @NonNull
    private SendAreaEntity loadDataDictionaryEntity(Pageable pageable, BedRoomEntity entity, String sendId) {
        SendAreaEntity result;

        List<SendAreaEntity> sendAreaEntities = sendAreaService.lambdaQuery()
                .eq(SendAreaEntity::getAreaCode, sendId)
                .or()
                .eq(SendAreaEntity::getId, sendId)
                .list();

        // 判断查找结果是否为空
        if (sendAreaEntities.isEmpty()) {
            result = new SendAreaEntity();
            result.setAreaName("未知区域");
            result.setChildren(Collections.emptyList());
            return result;
        }

        // 查找食物信息列表信息
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        if (sendAreaEntities.size() == 1) {
            result = sendAreaEntities.get(0);
            lambdaQuery.eq(BedRoomEntity::getAreaId, result.getId());
        } else {
            result = sendAreaEntities.get(0);
            List<String> stringList = sendAreaEntities.stream()
                    .map(SendAreaEntity::getId)
                    .collect(Collectors.toList());
            lambdaQuery.in(BedRoomEntity::getAreaId, stringList);
        }
        result.setChildren(lambdaQuery.list());
        return result;
    }

    @NonNull
    private LambdaQueryChainWrapper<BedRoomEntity> buildLambdaQuery(BedRoomEntity entity) {
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = bedRoomService.lambdaQuery();
        return lambdaQuery;
    }

}
